var R = require('ramda');
var _ = require('lodash');

var Maybe = function (x) {
  this.__value = x
}

Maybe.of = function (x) {
  return new Maybe(x)
}

Maybe.prototype.isNothing = function () {
  return (this.__value === null || this.__value === undefined)
}

Maybe.prototype.map = function (f) {
  if (this.isNothing()) {
    return Maybe.of(null)
  } else {
    return Maybe.of(f(this.__value))
  }
}

// 感觉是将一个值放到了一个容器中 并在使用的时候进行扩展
// var x = Maybe.of("Malkovich Malkovich").map(function(x) {return x});
// console.dir(x)

// maybe 实例
// var safeHead = function(xs) {
//   return Maybe.of(xs[0]);
// };

// var map = R.curry(function(f, any_functor_at_all) {
//   return any_functor_at_all.map(f);
// });
//
// var streetName = R.compose(map(_.prop('street')), safeHead, _.prop('addresses'));
// console.dir(streetName({addresses: []}))

// var arr = [1]
// var a = Maybe.of(arr[1])
// console.dir(a.map(function(v) { console.dir(v)}))

// 这里使用了 pointfree 风格(就是将函数进行串联调用) 这样就看不到形参本身
const map = R.curry((f, functor) => functor.map(f));
map((x) => {console.dir(x)})(Maybe.of(null));
